/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { YogaNative } from './YogaNative';
import { YogaLogger } from './YogaLogger';
import { YogaConfig } from './YogaConfig';
import { YogaExperimentalFeature } from './YogaExperimentalFeature';
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */


export abstract class YogaConfigNAPIBase extends YogaConfig {

  protected mNativePointer: number; //long
  private mLogger: YogaLogger;

  public constructor(nativePointer: number = YogaNative.napi_YGConfigNewNAPI()) { //long
    super()
    if (nativePointer == 0) {
      throw new Error("Failed to allocate native memory");
    }
    this.mNativePointer = nativePointer;
  }

//  YogaConfigNAPIBase() {
//    this(YogaNative.napi_YGConfigNewNAPI());
//  }
//
//  YogaConfigNAPIBase(boolean useVanillaNAPI) {
//    this(YogaNative.napi_YGConfigNewNAPI());
//  }

  public setExperimentalFeatureEnabled(feature: YogaExperimentalFeature, enabled: boolean): void {
    YogaNative.napi_YGConfigSetExperimentalFeatureEnabledNAPI(this.mNativePointer, feature as number, enabled);
  }

  public setUseWebDefaults(useWebDefaults: boolean): void {
    YogaNative.napi_YGConfigSetUseWebDefaultsNAPI(this.mNativePointer, useWebDefaults);
  }

  public setPrintTreeFlag(enable: boolean): void {
    YogaNative.napi_YGConfigSetPrintTreeFlagNAPI(this.mNativePointer, enable);
  }

  public setPointScaleFactor(pixelsInPoint: number): void {
    YogaNative.napi_YGConfigSetPointScaleFactorNAPI(this.mNativePointer, pixelsInPoint);
  }

  /**
   * Yoga previously had an error where containers would take the maximum space possible instead of the minimum
   * like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
   * Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
   */
  public setUseLegacyStretchBehaviour(useLegacyStretchBehaviour: boolean): void {
    YogaNative.napi_YGConfigSetUseLegacyStretchBehaviourNAPI(this.mNativePointer, useLegacyStretchBehaviour);
  }

  /**
   * If this flag is set then yoga would diff the layout without legacy flag and would set a bool in
   * YogaNode(mDoesLegacyStretchFlagAffectsLayout) with true if the layouts were different and false
   * if not
   */
  public setShouldDiffLayoutWithoutLegacyStretchBehaviour(
      shouldDiffLayoutWithoutLegacyStretchBehaviour: boolean): void {
      YogaNative.napi_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviourNAPI(
          this.mNativePointer, shouldDiffLayoutWithoutLegacyStretchBehaviour);
  }

  public setLogger(logger: YogaLogger): void {
    this.mLogger = logger;
    YogaNative.napi_YGConfigSetLoggerNAPI(this.mNativePointer, logger);
  }

  public getLogger(): YogaLogger {
    return this.mLogger;
  }

  getNativePointer(): number { //long
    return this.mNativePointer;
  }
}
